﻿using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;
using System;

/// <summary>
/// 使用Application.persistentDataPath方式来创建文件，读写Xml文件.
/// 注Application.persistentDataPath末尾没有“/”符号
/// </summary>
public class FileHelper
{

    private static FileHelper _instance;
    public static FileHelper Instance {
        get
        {
            if (_instance == null)
            {
                _instance = new FileHelper();
            }
            return _instance;
        }
    }

    /// <summary>
    /// 判断是否有文件
    /// </summary>
    /// <param name="path"></param>
    /// <param name="fileName"></param>
    /// <returns></returns>
    public bool IsFileExists(string path, string fileName)
    {
        FileInfo t = new FileInfo(path + fileName);
        if (!t.Exists)
        {
            //没有
            return false;
        } else
        {
            return true;
        }
    }


    public bool IsFileExists(string fileName)
    {
        FileInfo t = new FileInfo(Application.persistentDataPath + "/" + fileName);
        if (!t.Exists)
        {
            //没有
            return false;
        }
        else
        {
            return true;
        }
    }


    /// <summary>
    /// 动态创建文件夹.
    /// </summary>
    /// <returns>The folder.</returns>
    /// <param name="path">文件创建目录.</param>
    /// <param name="FolderName">文件夹名(不带符号).</param>
    public string CreateFolder(string path, string FolderName)
    {
        string FolderPath = path + FolderName;
        if (!Directory.Exists(FolderPath))
        {
            Directory.CreateDirectory(FolderPath);
        }
        return FolderPath;
    }

    public void DeleteFolder(string path, string FolderName)
    {
        string FolderPath = path + FolderName;
        if (Directory.Exists(FolderPath))
        {
            Directory.Delete(FolderPath,true);
        }
    }

    /// <summary>
    /// 创建文件.
    /// </summary>
    /// <param name="path">完整文件夹路径.</param>
    /// <param name="name">文件的名称.</param>
    /// <param name="info">写入的内容.</param>
    public void CreateFile(string path, string name, string info)
    {
        //文件流信息
        StreamWriter sw;
        FileInfo t = new FileInfo(path + name);
        if (!t.Exists)
        {
            //如果此文件不存在则创建
            sw = t.CreateText();
        }
        else
        {
            //如果此文件存在则打开
            sw = t.AppendText();
        }
        //以行的形式写入信息
        sw.WriteLine(info);
        //关闭流
        sw.Close();
        //销毁流
        sw.Dispose();
    }

    /// <summary>
    /// 创建图片
    /// </summary>
    /// <param name="name"></param>
    /// <param name="tex2d"></param>
    public void CreateImage(string FilePath, string name, Texture2D tex2d)
    {

        if (!Directory.Exists(FilePath))
        {
            Directory.CreateDirectory(FilePath);
        }
        tex2d.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, true);
        byte[] bytes = tex2d.EncodeToPNG();
        tex2d.Compress(true);
        tex2d.Apply();
        string path = FilePath + "/" + name;
        //byte[] imageByte = tex2d.EncodeToJPG();
        File.WriteAllBytes(path, bytes);
    }

    /// <summary>
    /// 读取文件.
    /// </summary>
    /// <returns>The file.</returns>
    /// <param name="path">完整文件夹路径.</param>
    /// <param name="name">读取文件的名称.</param>
    public ArrayList LoadFile(string path, string name)
    {
        //使用流的形式读取
        StreamReader sr = null;
        try
        {
            sr = File.OpenText(path + name);
        }
        catch (Exception e)
        {
            //路径与名称未找到文件则直接返回空
            return null;
        }
        string line;
        ArrayList arrlist = new ArrayList();
        while ((line = sr.ReadLine()) != null)
        {
            //一行一行的读取
            //将每一行的内容存入数组链表容器中
            arrlist.Add(line);
        }
        //关闭流
        sr.Close();
        //销毁流
        sr.Dispose();
        //将数组链表容器返回
        return arrlist;
    }

    

    /// <summary>
    /// 获取文件下所有文件大小
    /// </summary>
    /// <param name="filePath"></param>
    /// <returns></returns>
    public int GetAllFileSize(string filePath)
    {
        int sum = 0;
        if (!Directory.Exists(filePath))
        {
            return 0;
        }

        DirectoryInfo dti = new DirectoryInfo(filePath);

        FileInfo[] fi = dti.GetFiles();

        foreach (FileInfo f in fi)
        {

            sum += Convert.ToInt32(f.Length / 1024);
        }

        DirectoryInfo[] di = dti.GetDirectories();

        if (di.Length > 0)
        {
            for (int i = 0; i < di.Length; i++)
            {
                sum += GetAllFileSize(di[i].FullName);
            }
        }
        return sum;
    }

    /// <summary>
    /// 获取指定文件大小
    /// </summary>
    /// <param name="FilePath"></param>
    /// <param name="FileName"></param>
    /// <returns></returns>
    public int GetFileSize(string FilePath, string FileName)
    {
        int sum = 0;
        if (!Directory.Exists(FilePath))
        {
            return 0;
        }
        else
        {
            FileInfo Files = new FileInfo(@FilePath + FileName);
            sum += Convert.ToInt32(Files.Length / 1024);
        }
        return sum;
    }

    void CreateassetbundleFile(string path, string name, byte[] info, int length)
    {
        //文件流信息
        //StreamWriter sw;
        Stream sw;
        FileInfo t = new FileInfo(path + "//" + name);
        if (!t.Exists)
        {
            //如果此文件不存在则创建
            sw = t.Create();
        }
        else
        {
            //如果此文件存在则打开
            //sw = t.Append();
            return;
        }
        //以行的形式写入信息
        sw.Write(info, 0, length);
        //关闭流
        sw.Close();
        //销毁流
        sw.Dispose();
    }

    /// <summary>
    /// 删除文件.
    /// </summary>
    /// <param name="path">删除完整文件夹路径.</param>
    /// <param name="name">删除文件的名称.</param>
    public void DeleteFile(string path, string name)
    {
        File.Delete(path + name);
    }

    /// <summary>
    /// 删除文件
    /// </summary>
    /// <param name="path"></param>
    /// <param name="filesName"></param>
    /// <returns></returns>
    public bool DeleteFiles(string path, string filesName)
    {
        bool isDelete = false;
        try
        {
            if (Directory.Exists(path))
            {
                if (File.Exists(path + "\\" + filesName))
                {
                    File.Delete(path + "\\" + filesName);
                    isDelete = true;
                }
            }
        }
        catch
        {
            return isDelete;
        }
        return isDelete;
    }



    

    /// <summary>    
    /// 判断是否是目录文件    
    /// </summary>    
    /// <param name="path"></param>    
    /// <returns></returns>    
    private bool IsDirectory(string path)
    {
        if (path[path.Length - 1] == '/')
        {
            return true;
        }
        return false;
    }


    /// <summary>
    /// 以IO方式进行对图片进行加载
    /// </summary>
    public byte[] LoadImageByIO(string imagePath)
    {
        //double startTime = (double)Time.time;
        //创建文件读取流
        FileStream fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
        fileStream.Seek(0, SeekOrigin.Begin);
        //创建文件长度缓冲区
        byte[] bytes = new byte[fileStream.Length];
        //读取文件
        fileStream.Read(bytes, 0, (int)fileStream.Length);
        //释放文件读取流
        fileStream.Close();
        fileStream.Dispose();
        fileStream = null;

        return bytes;

       
    }

    /// <summary>
    /// 加载文件返回Byte[]
    /// </summary>
    /// <param name="path"></param>
    /// <param name="name"></param>
    /// <returns></returns>
    public byte[] LoadFileBytes(string path, string name)
    {
        string FixFilePath = path + name;
        byte[] bytes = System.IO.File.ReadAllBytes(FixFilePath);
        return bytes;
    }

}